Skip to content

fix: return defined false from stat/lstat in scalar context#105

Draft
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-stat-scalar-context
Draft

fix: return defined false from stat/lstat in scalar context#105
Koan-Bot wants to merge 1 commit intocpan-authors:mainfrom
Koan-Bot:koan.atoomic/fix-stat-scalar-context

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented May 7, 2026

What

Fix stack imbalance in pp_overload_stat when stat mock returns an empty array (file not found) and the caller uses scalar context.

Why

Perl's real pp_stat pushes &PL_sv_no (defined false) in scalar/void context on failure. The mock's failure path popped the argument but never pushed a return value, leaving the stack one element short. This caused stat($f) in scalar context to return undef instead of defined false.

Code using defined(stat($f)) or stat($f) // $default would observe different behavior under the mock vs real Perl.

How

Check GIMME_V in the size < 0 early-return path and push &PL_sv_no for non-list contexts, matching Perl's own pp_stat semantics.

Testing

  • New t/stat-scalar-context.t tests scalar context for both missing and present mocked files
  • Verifies defined() returns true on failure (matching real Perl)
  • Stack integrity check ensures no corruption of surrounding values
  • Full test suite passes

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 83 insertions(+), 1 deletion(-)

Code scan: clean

Tests: passed (0 Tests)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

pp_overload_stat's failure path (empty stat array) popped the argument
from the Perl stack but did not push a return value, causing a stack
imbalance in scalar context.  Real Perl's pp_stat pushes &PL_sv_no
(defined false) in scalar/void context when stat fails; the mock was
instead leaving the stack short, which caused scalar stat() to yield
undef rather than defined false.

Code using `defined(stat($f))` or `stat($f) // $default` would observe
different behavior under the mock vs real Perl.

Fix: check GIMME_V in the failure path and push &PL_sv_no for non-list
contexts, matching Perl's own pp_stat semantics.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant